<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>laravel框架学习笔记 | 学途路漫漫</title><meta name="author" content="学途路漫漫"><meta name="copyright" content="学途路漫漫"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="laravel54笔记1.准备前 切换阿里云镜像:composer config -g repo.packagist composer https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;composer&#x2F; laravel下载语句:composer create-project laravel&#x2F;laravel&#x3D;5.4.* --prefer-dist .&#x2F;laravel  2.目录结构分析 app">
<meta property="og:type" content="article">
<meta property="og:title" content="laravel框架学习笔记">
<meta property="og:url" content="http://example.com/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="学途路漫漫">
<meta property="og:description" content="laravel54笔记1.准备前 切换阿里云镜像:composer config -g repo.packagist composer https:&#x2F;&#x2F;mirrors.aliyun.com&#x2F;composer&#x2F; laravel下载语句:composer create-project laravel&#x2F;laravel&#x3D;5.4.* --prefer-dist .&#x2F;laravel  2.目录结构分析 app">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2023/04/03/pph4doQ.jpg">
<meta property="article:published_time" content="2020-12-01T12:06:29.000Z">
<meta property="article:modified_time" content="2023-04-03T11:25:54.927Z">
<meta property="article:author" content="学途路漫漫">
<meta property="article:tag" content="Php">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2023/04/03/pph4doQ.jpg"><link rel="shortcut icon" href="/img/avatar.gif"><link rel="canonical" href="http://example.com/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'laravel框架学习笔记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-03 19:25:54'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (false) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.gif" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 博客</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s1.ax1x.com/2023/04/03/pph4doQ.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="学途路漫漫"></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 博客</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">laravel框架学习笔记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-12-01T12:06:29.000Z" title="发表于 2020-12-01 20:06:29">2020-12-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-03T11:25:54.927Z" title="更新于 2023-04-03 19:25:54">2023-04-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/Php/">Php</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="laravel框架学习笔记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="laravel54笔记"><a href="#laravel54笔记" class="headerlink" title="laravel54笔记"></a>laravel54笔记</h1><h3 id="1-准备前"><a href="#1-准备前" class="headerlink" title="1.准备前"></a>1.准备前</h3><ul>
<li>切换阿里云镜像:<code>composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/</code></li>
<li>laravel下载语句:<code>composer create-project laravel/laravel=5.4.* --prefer-dist ./laravel</code></li>
</ul>
<h3 id="2-目录结构分析"><a href="#2-目录结构分析" class="headerlink" title="2.目录结构分析"></a>2.目录结构分析</h3><ul>
<li>app：核心目录，存放核心代码，控制器、模型、路由。模型文件直接写在app目录下即可，也可分目录管理</li>
<li>bootstarp（启动）：laravel启动目录，autoload.php文件用于自动载入需要类文件</li>
<li>config：项目配置目录，存放配置目录。例如数据库的配置文件<ul>
<li>App.php：项目主要配置文件</li>
<li>Auth.php：用于定义用户认证（登陆）配置文件</li>
<li>Database.php：针对数据库的配置</li>
<li>Filesystems.php：上传文件、文件存储需要使用到的配置文件</li>
</ul>
</li>
<li>database：数据迁移目录，存储跟数据表相关的操作类文件（迁移文件、种子文件<ul>
<li>migrations：创建数据表类</li>
<li>seeds：种子文件目录、</li>
</ul>
</li>
<li>public：项目入口文件和系统静态资源目录（css,img,js,uploads）后期使用的外部静态文件（js、css、图片等）都需要放到public。项目单一入口文件也在此目录下，和后续配置虚拟主机时需将站点位置指定到public下</li>
<li>resources：存放视图文件，还有语言包文件的目录<ul>
<li>assets：有用的</li>
<li>lang：语言包，如需本地化配置语言包</li>
<li>views：视图，可分目录管理</li>
</ul>
</li>
<li>routes：定义路由的目录，web.php是定义路由的文件</li>
<li>storage：存放缓存和日志文件，在linux下该目录需可写权限（后期用户上传文件如果存在本地也在此目录）<ul>
<li>app：用户文件</li>
<li>framework：缓存文件</li>
<li>logs：日志</li>
</ul>
</li>
<li>vendor：第三方代码依赖</li>
<li>.env文件：设置一些系统相关的环境配置文件信息。config目录里面的文件配置内容一般都是读取该文件里面的配置信息（config里面的配置项的值基本来自.env文件）</li>
<li><strong>artisan脚手架文件</strong>：主要用于生成的代码（自动生成），比如生成控制器，模型文件等<ul>
<li>执行命令：#php artisan (环境变量，artisan存在命令行当前工作路径)</li>
</ul>
</li>
<li>composer.json：依赖包配置文件，声明当前需要的软件依赖，不能删，composer需使用</li>
</ul>
<table>
<thead>
<tr>
<th>目录</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>app</td>
<td>保存模型文件（默认）</td>
</tr>
<tr>
<td>app&#x2F;Http&#x2F;Controllers</td>
<td>保存控制器文件</td>
</tr>
<tr>
<td>resources&#x2F;views</td>
<td>保存视图文件</td>
</tr>
<tr>
<td>config</td>
<td>配置文件目录</td>
</tr>
<tr>
<td>routes</td>
<td>存放路由文件</td>
</tr>
<tr>
<td>database&#x2F;migrations</td>
<td>存放数据库迁移文件（操作数据表结构）</td>
</tr>
<tr>
<td>database&#x2F;seeds</td>
<td>存放数据库种子文件（模拟测试数据）</td>
</tr>
</tbody></table>
<h3 id="3-启动方式"><a href="#3-启动方式" class="headerlink" title="3.启动方式"></a>3.启动方式</h3><p>方式一(不推荐)：Laravel框架提供了更简单的方式启动项目（相比配置apche）</p>
<ul>
<li>cmd执行命令：<code>#php artisan serve</code></li>
<li>缺点：能跑php，不启动数据库，如果修改项目配置文件，则需重新启动</li>
</ul>
<p>方式二(推荐)：使用wamp或lamp环境，虚拟主机配置（虚拟主机≠虚拟机）</p>
<h3 id="4-路由"><a href="#4-路由" class="headerlink" title="4.路由"></a>4.路由</h3><ul>
<li>什么是路由：将用户的请求按照事先规划的方案提交给指定的控制器或功能函数来进行处理（通俗的酱，路由就是访问地址形式）。在ThinkPHP框架中，当我们在URL地址中，传递m、c、a三个参数时，系统会自动跳转到指定模型中指定控制器的指定方法，这些处理过程都由框架自动完成。但在Laravel框架，并没有指定固定参数，其路由必须手工配置。</li>
</ul>
<h4 id="4-1-路由配置文件：routes-x2F-web-php"><a href="#4-1-路由配置文件：routes-x2F-web-php" class="headerlink" title="4.1.路由配置文件：routes&#x2F;web.php"></a>4.1.路由配置文件：routes&#x2F;web.php</h4><ul>
<li>路由定义格式：<strong>Route::请求方式(‘请求的url’,匿名函数或控制响应方法)</strong></li>
</ul>
<h4 id="4-2-路由方式"><a href="#4-2-路由方式" class="headerlink" title="4.2.路由方式"></a>4.2.路由方式</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);<span class="comment">//支持get请求方式</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">post</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);<span class="comment">//支持post请求方式</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="keyword">match</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);<span class="comment">//表示匹配固定（自己定义）请求方式</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">any</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);<span class="comment">//匹配任意请求方式</span></span><br><span class="line"><span class="comment">//下面不常用</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">put</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">delete</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">options</span>(<span class="variable">$url</span>,<span class="variable">$callback</span>);</span><br></pre></td></tr></table></figure>

<p>有时候还需要注册路由响应多个HTTP请求，这可以通过<code>match</code>方法来实现。或者用<code>any</code>方法注册一个路由来响应所有HTTP请求</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="keyword">match</span>([<span class="string">&#x27;get&#x27;</span>,<span class="string">&#x27;post&#x27;</span>],<span class="string">&#x27;/&#x27;</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//可以通过get和post方式请求/目录</span></span><br><span class="line">&#125;);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">any</span>(<span class="string">&#x27;foo&#x27;</span>,function()&#123;</span><br><span class="line">    <span class="comment">//可以通过任意请求方式访问/foo目录</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="4-3-路由参数"><a href="#4-3-路由参数" class="headerlink" title="4.3.路由参数"></a>4.3.路由参数</h4><ul>
<li>路由参数就是给路由地址传递参数</li>
</ul>
<p>路由参数分为必选参数和可选参数 </p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//必须传参，不传报错</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/user/&#123;id&#125;&#x27;</span>,function(<span class="variable">$id</span>)&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="string">&quot;当前用户id是&quot;</span>.<span class="variable">$id</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//有id也行 没id也行需要给默认值</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/user/&#123;id?&#125;&#x27;</span>,function(<span class="variable">$id</span>=<span class="literal">null</span>)&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="string">&quot;当前用户id是&quot;</span>.<span class="variable">$id</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/user/&#123;id?&#125;&#x27;</span>,function(<span class="variable">$id</span>=<span class="number">5</span>)&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="string">&quot;当前用户id是&quot;</span>.<span class="variable">$id</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">//参数字符不支持-可以用_代替</span></span><br></pre></td></tr></table></figure>

<p>还可以通过传统的?传参，不需要写路由</p>
<h4 id="4-4-路由别名"><a href="#4-4-路由别名" class="headerlink" title="4.4.路由别名"></a>4.4.路由别名</h4><p>路由别名相当于在路由定义时，为路由起了一个别名，在以后程序中可以通过这个别名来获取路由信息。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/user/&#123;id&#125;&#x27;</span>,function(<span class="variable">$id</span>)&#123;</span><br><span class="line">    <span class="comment">//xxx</span></span><br><span class="line">&#125;)-&gt;<span class="title function_ invoke__">name</span>(<span class="string">&#x27;名字&#x27;</span>);</span><br><span class="line"><span class="comment">//调用</span></span><br><span class="line"><span class="title function_ invoke__">route</span>(<span class="string">&#x27;名字&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>查看系统已有路由：php artisan</p>
<h4 id="4-5-路由群组"><a href="#4-5-路由群组" class="headerlink" title="4.5.路由群组"></a>4.5.路由群组</h4><p>比如后台有如下路由</p>
<p>&#x2F;admin&#x2F;login</p>
<p>&#x2F;admin&#x2F;logout</p>
<p>&#x2F;admin&#x2F;index</p>
<p>&#x2F;admin&#x2F;user&#x2F;add</p>
<p>他们共同点都有admin前缀，为了管理方便，可以把他们放到一个路由分组中，使用prefix属性指定路由前缀</p>
<p>比如你想为所有路由URIs前面添加前缀admin</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">group</span>([<span class="string">&#x27;prefix&#x27;</span>=&gt;<span class="string">&#x27;admin&#x27;</span>],function()&#123;</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;users&#x27;</span>,function()&#123;</span><br><span class="line">        <span class="comment">//匹配&quot;/admin/users&quot;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;logout&#x27;</span>,function()&#123;</span><br><span class="line">        <span class="comment">//匹配&quot;/admin/logout&quot;</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;user/add&#x27;</span>,function()&#123;</span><br><span class="line">        <span class="comment">//匹配&quot;/admin/user/add&quot;</span></span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>语法：Route::group(公共属性数组,回调函数)</p>
<h3 id="5-控制器"><a href="#5-控制器" class="headerlink" title="5.控制器"></a>5.控制器</h3><ul>
<li>控制器文件位于：app&#x2F;Http&#x2F;Controllers</li>
<li>控制器文件如何命名：大驼峰命名+Controller.php。例如：GoodsController.php</li>
</ul>
<h4 id="5-1-控制器生成"><a href="#5-1-控制器生成" class="headerlink" title="5.1.控制器生成"></a>5.1.控制器生成</h4><p><code>php artisan make:controller 控制器名(大驼峰)+Controller 关键词</code></p>
<p>例如：<code>php artisan make:controller TestController</code></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span> <span class="title class_">App</span>\<span class="title class_">Http</span>\<span class="title class_">Controllers</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Request</span>;<span class="comment">//命名空间三元素：常量、方法、类</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TestController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">//测试路由函数</span></span><br><span class="line">    <span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="title function_ invoke__">phpinfo</span>();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-1-控制器路由"><a href="#5-1-控制器路由" class="headerlink" title="5.1.控制器路由"></a>5.1.控制器路由</h4><ul>
<li>如何使用路由规则调用控制器下的方法</li>
<li>路由设置格式基本相同，只需将匿名函数换成<code>控制器类命@方法名</code></li>
<li>格式：Route::请求方法(路由表达式,控制器@方法)</li>
</ul>
<p>例子：设定路由home&#x2F;test&#x2F;test1</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//web文件下</span></span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;home/test/test1&#x27;</span>,<span class="string">&#x27;TestController@test1&#x27;</span>);</span><br></pre></td></tr></table></figure>

<ul>
<li>分目录管理：<ul>
<li>例：创建前台分组，在前台平台中创建IndexController.php文件；同时建立后台分组，再创建后台的IndexController.php</li>
<li>在Controllers文件夹下新建2个文件夹 Admin和Home</li>
<li>创建控制器文件命令<ul>
<li><code>php artisan make:controller Admin/IndexController</code></li>
<li><code>php artisan make:controller Home/IndexController</code></li>
</ul>
</li>
<li>路由文件：<ul>
<li><code>Route::get(&#39;home/index/index&#39;,&#39;Home\IndexController@index&#39;);</code></li>
<li><code>Route::get(&#39;admin/index/index&#39;,&#39;Admin\IndexController@index&#39;);</code></li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="6-控制器接受用户输入"><a href="#6-控制器接受用户输入" class="headerlink" title="6.控制器接受用户输入"></a>6.控制器接受用户输入</h3><ul>
<li>接受用户输入类：Illuminate\Support\Facades\Input</li>
<li>Facades:中文门面。门面是介于一个类的实例化与没有实例化中间的一个状态。其实是类的一个接口实现。在这个状态下可以不实例化类但是可以调用类中的方法。</li>
</ul>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;参数名字&#x27;</span>,<span class="string">&#x27;如果参数没被传递使用该默认值&#x27;</span>)</span><br><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">all</span>() <span class="comment">//获取所有用户输入</span></span><br><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;&#x27;</span>) <span class="comment">//获取单个的用户输入</span></span><br><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">only</span>([]) <span class="comment">//获取指定几个用户的输入</span></span><br><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">except</span>([]) <span class="comment">//获取指定几个用户的输入以外的所有参数</span></span><br><span class="line"><span class="title class_">Input</span>::<span class="title function_ invoke__">has</span>(<span class="string">&#x27;name&#x27;</span>) <span class="comment">//判断某个输入的参数是否存在</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//上述方法即可获取get也可获取post的信息，laravel7以上淘汰</span></span><br><span class="line"><span class="comment">//2.使用request类</span></span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Http</span>\<span class="title">Request</span>;</span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">insert</span>(<span class="params">Request <span class="variable">$res</span></span>)</span>&#123;</span><br><span class="line">        <span class="variable">$username</span>=<span class="variable">$res</span>-&gt;<span class="title function_ invoke__">input</span>(<span class="string">&#x27;username&#x27;</span>,<span class="string">&#x27;123&#x27;</span>);<span class="comment">//默认123</span></span><br><span class="line">        <span class="title function_ invoke__">dd</span>(<span class="variable">$username</span>);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>使用Input类前需先use引用，可以使用别名，配置在config文件夹下app.php。</p>
<h3 id="7-DB类操作数据库"><a href="#7-DB类操作数据库" class="headerlink" title="7.DB类操作数据库"></a>7.DB类操作数据库</h3><p>按照MVC架构，对数据操作应放在Model中完成，但如果不使用Model，我们也可以用laravel框架提供的DB类操作数据库。对于某些极其复杂sql，用Model已经很难完成，需要开发者自己手写sql语句，使用DB类去执行原生sql。laravel中DB类的基本用法DB::table(‘tableName’)获取tableName表的实例</p>
<h4 id="7-1-配置文件"><a href="#7-1-配置文件" class="headerlink" title="7.1.配置文件"></a>7.1.配置文件</h4><p>.env文件中配置数据库相关信息</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">DB_CONNECTION=mysql <span class="comment">//数据库类型</span></span><br><span class="line">DB_HOST=<span class="number">127.0</span>.<span class="number">0.1</span>   <span class="comment">//localhost</span></span><br><span class="line">DB_PORT=<span class="number">3306</span>		<span class="comment">//段鸥</span></span><br><span class="line">DB_DATABASE=homestead<span class="comment">//数据库名</span></span><br><span class="line">DB_USERNAME=homestead<span class="comment">//账号</span></span><br><span class="line">DB_PASSWORD=secret	<span class="comment">//密码</span></span><br></pre></td></tr></table></figure>

<p>也可以在config目录下database.php文件里面配置，使用env函数，表示先从env获取，获取失败使用env函数第二个参数。<strong>strict，严格模式选项，建议修改为false</strong></p>
<h4 id="7-2-使用"><a href="#7-2-使用" class="headerlink" title="7.2.使用"></a>7.2.使用</h4><ol>
<li><p>控制器中引入DB门面：<code>user DB</code></p>
</li>
<li><p>定义增删改查需要的路由，例如：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/test/add&#x27;</span>,<span class="string">&#x27;TestController@add&#x27;</span>);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/test/del&#x27;</span>,<span class="string">&#x27;TestController@del&#x27;</span>);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/test/update&#x27;</span>,<span class="string">&#x27;TestController@update&#x27;</span>);</span><br><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;/test/select&#x27;</span>,<span class="string">&#x27;TestController@select&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>使用group优化路由</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Route</span>::<span class="title function_ invoke__">group</span>([<span class="string">&#x27;prefix&#x27;</span>=&gt;<span class="string">&#x27;test&#x27;</span>],function()&#123;</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;add&#x27;</span>,<span class="string">&#x27;TestController@add&#x27;</span>);</span><br><span class="line">	<span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;del&#x27;</span>,<span class="string">&#x27;TestController@del&#x27;</span>);</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;update&#x27;</span>,<span class="string">&#x27;TestController@update&#x27;</span>);</span><br><span class="line">    <span class="title class_">Route</span>::<span class="title function_ invoke__">get</span>(<span class="string">&#x27;select&#x27;</span>,<span class="string">&#x27;TestController@select&#x27;</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
</li>
<li><h5 id="增加信息-insert"><a href="#增加信息-insert" class="headerlink" title="增加信息(insert)"></a>增加信息(insert)</h5><p>对数据库增加数据主要有2个函数insert()和insertGetId()</p>
<p>insert()可以同时添加一条或多条，返回值布尔</p>
<p>insertGetId()只能添加一条，返回自增的id</p>
<p>举例：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//指定数据表</span></span><br><span class="line">    <span class="variable">$db</span>=DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;TableName&#x27;</span>);</span><br><span class="line">    <span class="comment">//增加多条数据,增加一个一维，多个二维</span></span><br><span class="line">    <span class="variable">$rs</span>=<span class="variable">$db</span>-&gt;<span class="title function_ invoke__">insert</span>([</span><br><span class="line">        [</span><br><span class="line">            <span class="string">&#x27;name&#x27;</span> =&gt; <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;age =&gt; ‘23&#x27;</span></span><br><span class="line">        ],[</span><br><span class="line">            <span class="string">&#x27;name&#x27;</span> =&gt; <span class="string">&#x27;李四&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;age =&gt; ‘23&#x27;</span></span><br><span class="line">        ],[</span><br><span class="line">            <span class="string">&#x27;name&#x27;</span> =&gt; <span class="string">&#x27;王五&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;age =&gt; ‘23&#x27;</span></span><br><span class="line">        ]</span><br><span class="line">    ]);</span><br><span class="line">    <span class="comment">//insertGetId</span></span><br><span class="line">    <span class="variable">$db</span> -&gt; <span class="title function_ invoke__">insertGetId</span>([</span><br><span class="line">         <span class="string">&#x27;name&#x27;</span> =&gt; <span class="string">&#x27;张三&#x27;</span>,</span><br><span class="line">         <span class="string">&#x27;age =&gt; ‘23&#x27;</span></span><br><span class="line">    ]);</span><br><span class="line">    <span class="title function_ invoke__">var_dump</span>(<span class="variable">$rs</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>ps:链式&#x2F;连贯操作:<code>DB::table(&#39;TableName&#39;)-&gt;insert()</code></p>
</li>
<li><h5 id="修改信息-update"><a href="#修改信息-update" class="headerlink" title="修改信息(update)"></a>修改信息(update)</h5><p>Update表示可以修改整个记录中的全部字段</p>
<p>Increment和decrement表示修改数字字段的数字(递增或递减),应用：记录登陆次数、积分增加</p>
<p>举例：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">update</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="variable">$db</span> = DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;TableName&#x27;</span>);</span><br><span class="line">    <span class="comment">//id=1的name改成&#x27;赵六&#x27;</span></span><br><span class="line">    <span class="variable">$rs</span>=<span class="variable">$db</span>-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;=&#x27;</span>,<span class="string">&#x27;1&#x27;</span>)-&gt;<span class="title function_ invoke__">update</span>([</span><br><span class="line">        <span class="string">&#x27;name&#x27;</span> =&gt; <span class="string">&#x27;赵六&#x27;</span></span><br><span class="line">    ]);</span><br><span class="line">    <span class="title function_ invoke__">dd</span>(<span class="variable">$rs</span>)<span class="comment">//更好的输出函数dd，返回受影响行数</span></span><br><span class="line">        <span class="comment">//======================</span></span><br><span class="line">    DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;tableName&#x27;</span>)-&gt;<span class="title function_ invoke__">increment</span>(<span class="string">&#x27;votes&#x27;</span>);<span class="comment">//每次+1</span></span><br><span class="line">    DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;tableName&#x27;</span>)-&gt;<span class="title function_ invoke__">increment</span>(<span class="string">&#x27;votes&#x27;</span>,<span class="number">5</span>);<span class="comment">//+5</span></span><br><span class="line">    DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;tableName&#x27;</span>)-&gt;<span class="title function_ invoke__">decrement</span>(<span class="string">&#x27;votes&#x27;</span>);<span class="comment">//-1</span></span><br><span class="line">    DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;tableName&#x27;</span>)-&gt;<span class="title function_ invoke__">decrement</span>(<span class="string">&#x27;votes&#x27;</span>,<span class="number">5</span>);<span class="comment">//-5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><h5 id="查询数据"><a href="#查询数据" class="headerlink" title="查询数据"></a>查询数据</h5><ol>
<li><p>取出基本数据</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取member表中所有数据</span></span><br><span class="line"><span class="variable">$db</span>=DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)</span><br><span class="line"><span class="variable">$date</span>=<span class="variable">$db</span>-&gt;<span class="title function_ invoke__">get</span>();<span class="comment">//返回集合对象</span></span><br><span class="line"><span class="comment">//遍历数据</span></span><br><span class="line"><span class="keyword">foreach</span>(<span class="variable">$data</span> <span class="keyword">as</span> <span class="variable">$key</span> =&gt; <span class="variable">$value</span>)&#123;</span><br><span class="line">    <span class="keyword">echo</span> <span class="string">&quot;value:<span class="subst">&#123;$value-&gt;id&#125;</span>&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//查id大于3</span></span><br><span class="line"><span class="variable">$date</span>=<span class="variable">$db</span>-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;&gt;&#x27;</span>,<span class="string">&#x27;3&#x27;</span>)-&gt;<span class="title function_ invoke__">get</span>();</span><br><span class="line"><span class="comment">//并且</span></span><br><span class="line">-&gt;<span class="title function_ invoke__">where</span>()-&gt;<span class="title function_ invoke__">where</span>()-&gt;<span class="title function_ invoke__">where</span>()</span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line">-&gt;<span class="title function_ invoke__">where</span>()-&gt;<span class="title function_ invoke__">orWhere</span>()-&gt;<span class="title function_ invoke__">orWhere</span>()</span><br></pre></td></tr></table></figure>
</li>
<li><p>取出单行数据</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;3&#x27;</span>)-&gt;<span class="title function_ invoke__">first</span>();<span class="comment">//返回一个对象</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>取出具体某个值(一个字段)</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//取出id为3的记录的name值</span></span><br><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;3&#x27;</span>)-&gt;<span class="title function_ invoke__">value</span>(<span class="string">&#x27;name&#x27;</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>获取某些字段数据(多个字段)</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">select</span>(<span class="string">&#x27;name as username&#x27;</span>,<span class="string">&#x27;email&#x27;</span>)-&gt;<span class="title function_ invoke__">get</span>();</span><br></pre></td></tr></table></figure>
</li>
<li><p>排序</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">orderBy</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;desc&#x27;</span>)-&gt;<span class="title function_ invoke__">get</span>();</span><br></pre></td></tr></table></figure>
</li>
<li><p>分页（限制输出记录数）</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">limit</span>(<span class="number">3</span>)-&gt;<span class="title function_ invoke__">offset</span>(<span class="number">2</span>)-&gt;<span class="title function_ invoke__">get</span>();</span><br><span class="line"><span class="comment">//Limit：限制输出条数</span></span><br><span class="line"><span class="comment">//Offset:从什么地方开始</span></span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><h5 id="删除数据-delete"><a href="#删除数据-delete" class="headerlink" title="删除数据(delete)"></a>删除数据(delete)</h5><p>删除两种方式：物理删除（本质是删除）<strong>逻辑删除（本质是修改）</strong></p>
<p>delete函数：删除记录</p>
<p>Truncate：清空数据表</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;member&#x27;</span>)-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;3&#x27;</span>)-&gt;<span class="title function_ invoke__">delete</span>();<span class="comment">//返回影响行数</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>执行任意sql语句</p>
<ol>
<li><p>执行任意insert update delete</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DB::<span class="title function_ invoke__">statement</span>(<span class="string">&quot;insert into member values(null,&quot;</span><span class="string">&quot;)&quot;</span>);</span><br></pre></td></tr></table></figure>
</li>
<li><p>执行任意select语句</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">$res</span>=DB::<span class="title function_ invoke__">select</span>(<span class="string">&quot;select * from member&quot;</span>);</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<h3 id="8-视图操作resources-x2F-views"><a href="#8-视图操作resources-x2F-views" class="headerlink" title="8.视图操作resources&#x2F;views"></a>8.视图操作resources&#x2F;views</h3><p>其实就是-html页面把控制器中分配的数据进行处理和展示</p>
<h4 id="8-1-视图文件命名与渲染"><a href="#8-1-视图文件命名与渲染" class="headerlink" title="8.1.视图文件命名与渲染"></a>8.1.视图文件命名与渲染</h4><ol>
<li>文件名习惯小写（建议小写）</li>
<li>文件名后缀blade.php（因为laravel里面有一套模板引擎就是使用blade，可以直接使用标签语法）,也可以使用原生php语法显示数据）</li>
<li>也可以不使用blade，这样话就没有模板引擎了，两个视图文件同时存在以模板引擎后缀优先显示</li>
<li>展示视图：控制器中调用return view(‘viewname’);</li>
<li>视图支持分目录管理。控制器中可以使用&#x2F;或.进行目录分隔</li>
</ol>
<h4 id="8-2-变量分配与展示（数据交互"><a href="#8-2-变量分配与展示（数据交互" class="headerlink" title="8.2.变量分配与展示（数据交互"></a>8.2.变量分配与展示（数据交互</h4><p>语法：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_ invoke__">view</span>(viewname,数组)</span><br><span class="line"><span class="title function_ invoke__">view</span>(viewname)-&gt;<span class="title function_ invoke__">with</span>(数组)</span><br><span class="line"><span class="title function_ invoke__">view</span>(viewname)-&gt;<span class="title function_ invoke__">with</span>(名称，值)-&gt;<span class="title function_ invoke__">with</span>(名称,值)</span><br><span class="line"><span class="comment">//举例 控制器中传参$date,$day</span></span><br><span class="line"><span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;testview&#x27;</span>,[<span class="string">&#x27;date&#x27;</span>=&gt;<span class="variable">$date</span>,<span class="string">&#x27;day&#x27;</span>=&gt;<span class="variable">$day</span>]);</span><br><span class="line">    <span class="comment">//调用 视图中展示</span></span><br><span class="line">	&#123;&#123;<span class="variable">$date</span>&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>模板输出：</p>
<h4 id="8-3-补充：compact函数传递参数"><a href="#8-3-补充：compact函数传递参数" class="headerlink" title="8.3.补充：compact函数传递参数"></a>8.3.补充：compact函数传递参数</h4><p>Compact函数是php内置函数 ，跟laravel框架没关系,用于打包数组</p>
<p>语法:compact(‘变量名1’,’变量名2’,……)</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//优化上面案例</span></span><br><span class="line"><span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;testview&#x27;</span>,<span class="title function_ invoke__">compact</span>(<span class="string">&#x27;date&#x27;</span>,<span class="string">&#x27;day&#x27;</span>));</span><br></pre></td></tr></table></figure>

<h4 id="8-4-在视图中使用函数"><a href="#8-4-在视图中使用函数" class="headerlink" title="8.4.在视图中使用函数"></a>8.4.在视图中使用函数</h4><p>在视图模板中我们可以使用函数去处理变量</p>
<p>语法：<code>&#123;&#123;函数名(参数1,参数2)&#125;&#125;</code></p>
<p>说明：函数名可以是php内置的，也可以是laravel框架中定义的</p>
<p>案例：在数据库中存储时间都是以时间戳存储，但在页面上展示不适合使用时间戳。需要对其进行格式化处理。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//控制器传递时间戳</span></span><br><span class="line"><span class="variable">$time</span>=<span class="title function_ invoke__">strtotime</span>(<span class="string">&#x27;+1 year&#x27;</span>);</span><br><span class="line"><span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;test&#x27;</span>,<span class="title function_ invoke__">compact</span>(<span class="string">&#x27;time&#x27;</span>));</span><br><span class="line"><span class="comment">//test模板</span></span><br><span class="line">&#123;&#123;<span class="title function_ invoke__">date</span>(<span class="string">&#x27;Y-m-d H:i:s&#x27;</span>,<span class="variable">$time</span>)&#125;&#125;</span><br></pre></td></tr></table></figure>

<h4 id="8-5-循环与分支语法标签"><a href="#8-5-循环与分支语法标签" class="headerlink" title="8.5.循环与分支语法标签"></a>8.5.循环与分支语法标签</h4><p>视图中遍历数据</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//php循环</span></span><br><span class="line"><span class="keyword">foreach</span>(<span class="variable">$variable</span> <span class="keyword">as</span> <span class="variable">$key</span> =&gt; <span class="variable">$value</span>)&#123;</span><br><span class="line">    <span class="comment">//循环体</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//laravel视图中</span></span><br><span class="line">@<span class="keyword">foreach</span>(<span class="variable">$variable</span> <span class="keyword">as</span> <span class="variable">$key</span> =&gt; <span class="variable">$value</span>)</span><br><span class="line">    <span class="comment">//循环体</span></span><br><span class="line">@<span class="keyword">endforeach</span></span><br><span class="line"></span><br><span class="line">@<span class="keyword">if</span>(条件)</span><br><span class="line">    语句<span class="number">1</span></span><br><span class="line">@<span class="keyword">elseif</span>(条件)</span><br><span class="line">    语句<span class="number">2</span></span><br><span class="line">@<span class="keyword">else</span></span><br><span class="line">    语句<span class="number">3</span></span><br><span class="line">@<span class="keyword">endif</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">//举例</span></span><br><span class="line"><span class="comment">//控制器</span></span><br><span class="line"><span class="variable">$data</span>=DB::<span class="title function_ invoke__">table</span>(<span class="string">&#x27;testTable&#x27;</span>)-&gt;<span class="title function_ invoke__">get</span>();</span><br><span class="line"><span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;test&#x27;</span>,<span class="title function_ invoke__">compact</span>(<span class="string">&#x27;data&#x27;</span>));</span><br><span class="line"><span class="comment">//test模板</span></span><br><span class="line">@<span class="keyword">foreach</span>(<span class="variable">$date</span> <span class="keyword">as</span> <span class="variable">$val</span>)</span><br><span class="line">	id:&#123;&#123;<span class="variable">$val</span>-&gt;id&#125;&#125;，name:&#123;&#123;<span class="variable">$val</span>-&gt;name&#125;&#125;</span><br><span class="line">@<span class="keyword">endforeach</span></span><br></pre></td></tr></table></figure>

<h4 id="8-6-模板继承-x2F-包含"><a href="#8-6-模板继承-x2F-包含" class="headerlink" title="8.6.模板继承&#x2F;包含"></a>8.6.模板继承&#x2F;包含</h4><p>用于制作公共部分页面</p>
<p>语法：@yield(‘名字’) 在父级页面中的占位</p>
<p>父级页面parent</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>头部<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line">@yield(&#x27;mainbody&#x27;)</span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>尾部<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>子级界面</p>
<p>语法：@extends(‘父级文件名’)	 相对路径根view</p>
<p>​			通过section标签绑定区块&#x2F;部件到父级页面，区块名称就是父级页面yield标签参数名</p>
<p>​			@section(区块名称)</p>
<p>​			代码</p>
<p>​			@endsection</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@extends(‘parent&#x27;)</span><br><span class="line">@section(&#x27;mainbody&#x27;)</span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    正文内容</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">@endsection</span><br></pre></td></tr></table></figure>

<p>模板包含：@include(‘模板文件名’)</p>
<p>继承模板留空：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;@<span class="keyword">yield</span>(<span class="string">&#x27;title&#x27;</span>)&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">@<span class="keyword">yield</span>(<span class="string">&#x27;content&#x27;</span>)</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<p>填空：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@<span class="keyword">extends</span>(<span class="string">&#x27;wap.base&#x27;</span>)</span><br><span class="line"></span><br><span class="line">@<span class="title function_ invoke__">section</span>(<span class="string">&#x27;title&#x27;</span>)</span><br><span class="line">    aa</span><br><span class="line">@endsection</span><br><span class="line"></span><br><span class="line">@<span class="title function_ invoke__">section</span>(<span class="string">&#x27;content&#x27;</span>)</span><br><span class="line">    &lt;h1&gt;我是aa&lt;/h1&gt;</span><br><span class="line">@endsection</span><br></pre></td></tr></table></figure>



<h4 id="8-7-外部静态文件引入"><a href="#8-7-外部静态文件引入" class="headerlink" title="8.7.外部静态文件引入"></a>8.7.外部静态文件引入</h4><p>原先</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/css/app.css&quot;</span>&gt;</span></span><br><span class="line">源码中显示效果href=&quot;/css/app.css&quot;</span><br></pre></td></tr></table></figure>

<p>现在laravel中系统封装了asset方法</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span> <span class="attr">href</span>=<span class="string">&quot;&#123;&#123;asset(&#x27;css/app.css&#x27;)&#125;&#125;&quot;</span>&gt;</span></span><br><span class="line">源码中显示效果href=&quot;域名/css/app.css&quot;</span><br></pre></td></tr></table></figure>

<h3 id="9-CSRF攻击"><a href="#9-CSRF攻击" class="headerlink" title="9.CSRF攻击"></a>9.CSRF攻击</h3><h4 id="9-1-什么是CSRF攻击"><a href="#9-1-什么是CSRF攻击" class="headerlink" title="9.1.什么是CSRF攻击"></a>9.1.什么是CSRF攻击</h4><p>CSRF是跨站请求伪造(Cross-site request forgery)的英文缩写</p>
<p>Laravel框架中避免CSRF攻击很简单，Laravel自动为每个用户Session生成了一个CSRF Token，该Token可用于验证登陆用户和发起请求者是否是同一人，如果不是则请求失败。和验证码原理一致</p>
<p>Laravel提供了一个全局帮助函数csrf token来获取该Token值，因此只需在视图提交中添加如下HTML代码即可在请求中带上Token</p>
<p>php模板：<code> &lt;input type=&quot;hidden&quot; name=&quot;_token&quot; value=&quot;&lt;?php echo csrf_token();&gt;&quot;&gt;</code></p>
<p>blade模板：<code>&lt;input type=&quot;hidden&quot; name=&quot;_token&quot; value=&quot;&#123;&#123;csrf_token()&#125;&#125;&quot;&gt;</code></p>
<h3 id="简写：-csrf-field"><a href="#简写：-csrf-field" class="headerlink" title="简写：{ {csrf_field()} }"></a><strong>简写：<code>&#123; &#123;csrf_field()&#125; &#125;</code></strong></h3><p><strong>异步提交</strong>时没有选择权，只能使用<code>csrf_token：$get(url,&#123;csrf_token;&#125;);</code></p>
<h4 id="9-2-CSRF验证排除例外路由"><a href="#9-2-CSRF验证排除例外路由" class="headerlink" title="9.2.CSRF验证排除例外路由"></a>9.2.CSRF验证排除例外路由</h4><p>不是所有请求都需要避免CSRF攻击，比如第三方api获取数据请求</p>
<p>可以通过在VerifyCsrfToken（app&#x2F;Http&#x2F;Middleware&#x2F;VerifyCsrfToken.php)中间件将要排除的请求URL添加到$except属性数组中</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> <span class="variable">$except</span>=[</span><br><span class="line">  <span class="comment">//此处填写需要排除csrf验证的路由</span></span><br><span class="line">    <span class="string">&#x27;home/test&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;home/test2&#x27;</span>,</span><br><span class="line">  <span class="comment">//星号排除全部</span></span><br><span class="line">	<span class="string">&#x27;*&#x27;</span></span><br><span class="line">];</span><br></pre></td></tr></table></figure>

<h3 id="10-模型操作-AR模式"><a href="#10-模型操作-AR模式" class="headerlink" title="10.模型操作(AR模式)"></a>10.模型操作(AR模式)</h3><p>Laravel自带的Eloquent ORM提供了一个美观、简单的与数据库打交道的ActiveRecord实现，<strong>每张数据表都对应一个与该表进行交互的“Model模型”</strong>，模型允许你在表中进行数据查询，以及插入、更新、删除等操作</p>
<blockquote>
<p>AR模型三核心(映射)：</p>
<p>每个数据表	与数据表进行交互的Model模型映射(实例化模型)</p>
<p>记录中的字段	与模型类的属性映射(给属性赋值)</p>
<p>表中的每个记录 	与一个完整的请求实例映射(具体的CURD操作)</p>
</blockquote>
<h4 id="10-1-定义模型"><a href="#10-1-定义模型" class="headerlink" title="10.1.定义模型"></a>10.1.定义模型</h4><ol>
<li><p>位置：app目录下，建议分目录管理</p>
</li>
<li><p>命名规则：一般使用 表名(首字母大写).php</p>
</li>
<li><p>创建模型：php artisan make:model Models&#x2F;Member</p>
</li>
<li><p><strong>定义模型注意事项</strong>：</p>
<ol>
<li>定义$table属性，值为不要前缀的表名，如果不指定则使用类名的复数形式作为表名，不定义默认找members表。修饰词:protected</li>
<li>定义$primarKey属性，值为主键名称，如果需要使用AR模式find方法，则可能需要指定主键(Model::find(n))。修饰词:protected</li>
<li>定义$timestamps属性，值为false，如果不设置false，则默认会操作表中的created_at和updated_at字段，我们表中一般没有，所以设置false，表示不操作这两个字段。修饰词:public</li>
<li>定义$fillable属性，表示使用模型插入数据时，允许插入到数据库的字段信息。修饰词:protected</li>
<li>使用模型create插入数据，设置$fillable允许入库字段，使用$guarded是设置排除入库字段</li>
</ol>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">namespace</span> <span class="title class_">App</span>\<span class="title class_">Home</span>;</span><br><span class="line"><span class="keyword">use</span> <span class="title">Illuminate</span>\<span class="title">Database</span>\<span class="title">Eloquent</span>\<span class="title">Model</span>;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Member</span> <span class="keyword">extends</span> <span class="title">Model</span></span>&#123;</span><br><span class="line">    <span class="comment">//定义模型关联数据表，一个模型一个表</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$table</span>-<span class="string">&#x27;member&#x27;</span>;</span><br><span class="line">    <span class="comment">//定义主键，可选</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$primaryKey</span>=<span class="string">&#x27;id&#x27;</span>;</span><br><span class="line">    <span class="comment">//定义禁止操作时间</span></span><br><span class="line"> <span class="keyword">public</span> <span class="variable">$timestamps</span>=<span class="literal">false</span>;</span><br><span class="line">    <span class="comment">//设置允许写入的数据字段</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$fillable</span>=[<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;age&#x27;</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="10-2-模型控制器调用"><a href="#10-2-模型控制器调用" class="headerlink" title="10.2.模型控制器调用"></a>10.2.模型控制器调用</h4><p>引入Member模型类，use App\Home\Member</p>
<p>模型在控制器中使用方式有2种</p>
<ol>
<li>直接像使用DB门面一样的操作方式，以调用静态方法为主的形式，该形式模型下不需要实例化，例如：Member::get() 等价于DB::table(‘member’)-&gt;get();</li>
<li>实例化模型然后再使用模型类(普通)，例如：$model&#x3D;new Member();$model-&gt;get();</li>
</ol>
<h4 id="10-3-添加数据"><a href="#10-3-添加数据" class="headerlink" title="10.3.添加数据"></a>10.3.添加数据</h4><ol>
<li><p>AR模式：必须实例化模型</p>
<p>$member&#x3D;new Member(); &#x2F;&#x2F;映射关系1，将表映射到模型</p>
<p>$member-&gt;name&#x3D;value; &#x2F;&#x2F;映射关系2，将字段映射打属性，属性名和字段名一致</p>
<p>$member-&gt;age&#x3D;value;&#x2F;&#x2F;映射关系3：将记录映射到实例</p>
</li>
</ol>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">add</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   	<span class="comment">//实例化模型 表类映射</span></span><br><span class="line">    <span class="variable">$model</span>=<span class="keyword">new</span> <span class="title class_">Member</span>();</span><br><span class="line">    <span class="comment">//属性赋值 字段类属性映射</span></span><br><span class="line">    <span class="variable">$model</span>-&gt;name=<span class="string">&#x27;zhangsan&#x27;</span>;</span><br><span class="line">    <span class="variable">$model</span>-&gt;age=<span class="string">&#x27;15&#x27;</span>;</span><br><span class="line">    <span class="comment">//具体操作 记录映射实例</span></span><br><span class="line">    <span class="variable">$model</span>-&gt;<span class="title function_ invoke__">save</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>不推荐，一个个字段添加</p>
<ol>
<li><p>推荐，可以使用数组</p>
<p>建立简单表单，提交姓名年龄</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params">Request <span class="variable">$request</span></span>)</span>&#123;</span><br><span class="line">	<span class="variable">$model</span>=<span class="keyword">new</span> <span class="title class_">Member</span>();</span><br><span class="line">    <span class="comment">//添加</span></span><br><span class="line">    <span class="variable">$result</span>=<span class="variable">$model</span>-&gt;<span class="title function_ invoke__">create</span>(<span class="variable">$request</span>-&gt;<span class="title function_ invoke__">all</span>());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>request语法</p>
<p>$request-&gt;all() 返回数组</p>
<p>$request-&gt;input(‘name’)</p>
<p>$request-&gt;only([‘name1’,’name2])</p>
<p>$request-&gt;except([‘name1’,’name2])</p>
<p>$request-&gt;has(‘name’)</p>
<p>$request-&gt;get(‘name’)</p>
</li>
</ol>
<h4 id="10-4-查询数据"><a href="#10-4-查询数据" class="headerlink" title="10.4.查询数据"></a>10.4.查询数据</h4><p>获取指定主键一条数据</p>
<p>$info&#x3D;Member::find(4); &#x2F;&#x2F;获取主键为4的数据</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test2</span>(<span class="params">Request <span class="variable">$request</span></span>)</span>&#123;</span><br><span class="line">	<span class="variable">$info</span>=<span class="title class_">Member</span>::<span class="title function_ invoke__">find</span>(<span class="number">4</span>);</span><br><span class="line">    <span class="keyword">echo</span> <span class="variable">$info</span>-&gt;name;<span class="comment">//结果为对象</span></span><br><span class="line">    <span class="comment">//如果需要将对象结果集转数组</span></span><br><span class="line">    -&gt;<span class="title function_ invoke__">get</span>()-&gt;<span class="title function_ invoke__">toArray</span>();</span><br><span class="line">    <span class="comment">//如下</span></span><br><span class="line">    <span class="variable">$info</span>=<span class="title class_">Member</span>::<span class="title function_ invoke__">find</span>(<span class="number">4</span>)-&gt;<span class="title function_ invoke__">toArray</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>获取符合指定条件的第一条记录</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test3</span>(<span class="params">Request <span class="variable">$request</span></span>)</span>&#123;</span><br><span class="line">    <span class="variable">$rest</span>=<span class="title class_">Member</span>::<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;&gt;&#x27;</span>,<span class="string">&#x27;4&#x27;</span>)-&gt;<span class="title function_ invoke__">toArray</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>查询多行并指定字段</p>
<p>Member::all()		Member::all(字段1，字段2);</p>
<p>Member::get() 	Member::get([字段1，字段2]);</p>
<p>按条件查询指定多个字段<br>Member::where(‘id’,’&gt;’,’4’)-&gt;get([列1,列2]);	数组选列</p>
<p>Member::where(‘id’,’&gt;’,’4’)-&gt;select(列1,列2)-&gt;get();  字符串选列</p>
<p>Member::where(‘id’,’&gt;’,’4’)-&gt;select([列1,列2])-&gt;get();  字符串选列</p>
<h4 id="10-4-修改数据"><a href="#10-4-修改数据" class="headerlink" title="10.4.修改数据"></a>10.4.修改数据</h4><p><strong>注意：在laravel里面如果需要更新数据（ORM模型方式），需要先调用模型的find方法获取对应记录，返回一个模型对象，然后为该模型对象设置要更新的数据（对象的属性）。最后调用save方法即可。</strong></p>
<p>例如：</p>
<p>$user&#x3D;User::find($id);</p>
<p>$user-&gt;title&#x3D;$_POST[‘title’];</p>
<p>$user-&gt;content&#x3D;$_POST[‘content’];</p>
<p>$user-&gt;save()</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test4</span>(<span class="params">Request <span class="variable">$request</span></span>)</span>&#123;</span><br><span class="line">    <span class="variable">$rest</span>=<span class="variable">$model</span>-&gt;<span class="title function_ invoke__">where</span>(<span class="string">&#x27;id&#x27;</span>,<span class="number">7</span>)-&gt;<span class="title function_ invoke__">update</span>([<span class="string">&#x27;age&#x27;</span>-&gt;‘<span class="number">10</span><span class="string">&#x27;]);</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="10-5-删除数据"><a href="#10-5-删除数据" class="headerlink" title="10.5.删除数据"></a>10.5.删除数据</h4><p><strong>注意：在laravel里面如果要删除数据，如果需要使用AR模型删除数据必须先根据主键id查询对应记录，返回模型对象，然后调用模型对象的delete</strong></p>
<p>例如代码：</p>
<p>$user&#x3D;User::find($id);</p>
<p>$uesr-&gt;delete();</p>
<h3 id="11-自动验证"><a href="#11-自动验证" class="headerlink" title="11.自动验证"></a>11.自动验证</h3><p>前端可通过JS验证表单数据有效性，但如果用户浏览器过低或直接禁用js，则前端验证可能失效，这样就不能保证数据有效性。所以后端也需要做相应的验证操作，这个操作在laravel中称为自动验证</p>
<h4 id="11-1-准备工作"><a href="#11-1-准备工作" class="headerlink" title="11.1.准备工作"></a>11.1.准备工作</h4><p>可以使用有表单的视图，建好路由控制器方法跳转到视图</p>
<h4 id="11-2-验证方法"><a href="#11-2-验证方法" class="headerlink" title="11.2.验证方法"></a>11.2.验证方法</h4><ol>
<li><p>基本语法</p>
<p>使用控制器中的validate方法来完成，$this-&gt;validate($request,[验证规则];)如果验证失败，laravel会自动将用户重新定向回上一个位置，并将验证错误信息一次性存放到session中</p>
</li>
</ol>
<p>扩展：如何得知请求类型。</p>
<p>语法：$request-&gt;method()   获取当前请求方法 返回GET或POST</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test13</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="variable">$request</span>-&gt;<span class="title function_ invoke__">method</span>()==<span class="string">&quot;POST&quot;</span>)&#123;</span><br><span class="line">     	</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;test&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol>
<li><p>基本规则</p>
<p>required:不能为空</p>
<p>max:255 最长255字符</p>
<p>min:1 最少1字符</p>
<p>email:验证邮箱是否合法</p>
<p>confirmed:验证两个字段是否相同,如果验证的字段是password，则必须输入一个与之匹配的password_confirmation字段</p>
<p>integer:验证字段必须是整型</p>
<p>numeric:字段必须数值</p>
<p>ip:必须是ip</p>
<p>size:value 验证字段必须有和给定value相匹配的尺寸，字符串是字符数，数值是给定整型值，文件对应文件字节数,固定值</p>
<p>string:必须是字符串</p>
<p>unique:表名，字段，需要排除的ID</p>
<p>alpha:必须字母</p>
<p><strong>多个验证规则通过“|”字符进行隔开</strong></p>
<p>语法：$this-&gt;validat(数据对象,[数组形式验证规则]);</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test13</span>(<span class="params">Request <span class="variable">$request</span></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="variable">$request</span>-&gt;<span class="title function_ invoke__">method</span>()==<span class="string">&quot;POST&quot;</span>)&#123;</span><br><span class="line">     	<span class="variable language_">$this</span>-&gt;<span class="title function_ invoke__">validate</span>(<span class="variable">$request</span>,[</span><br><span class="line">            <span class="string">&#x27;name&#x27;</span>=&gt;<span class="string">&#x27;required|min:2|max:20&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;age&#x27;</span>=&gt;<span class="string">&#x27;required|integer|min:1|max:120&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;email&#x27;</span>=&gt;<span class="string">&#x27;required|email&#x27;</span></span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;test&#x27;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="11-3-错误信息输出（放视图文件里头"><a href="#11-3-错误信息输出（放视图文件里头" class="headerlink" title="11.3.错误信息输出（放视图文件里头"></a>11.3.错误信息输出（放视图文件里头</h4><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">@<span class="keyword">if</span>(<span class="title function_ invoke__">count</span>(<span class="variable">$errors</span>)&gt;<span class="number">0</span>)</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span>=&quot;<span class="title">alert</span> <span class="title">alert</span>-<span class="title">danger</span>&quot;&gt;</span></span><br><span class="line"><span class="class">    	&lt;<span class="title">ul</span>&gt;</span></span><br><span class="line"><span class="class">    		@<span class="title">foreach</span>($<span class="title">errors</span>-&gt;<span class="title">all</span>() <span class="title">as</span> $<span class="title">error</span>)</span></span><br><span class="line"><span class="class">    			&lt;<span class="title">li</span>&gt;</span>&#123;&#123;<span class="variable">$error</span>&#125;&#125;&lt;/li&gt;</span><br><span class="line">    		@<span class="keyword">endforeach</span></span><br><span class="line">    	&lt;/ul&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">@<span class="keyword">endif</span></span><br></pre></td></tr></table></figure>

<h4 id="11-4-错误信息英转中"><a href="#11-4-错误信息英转中" class="headerlink" title="11.4.错误信息英转中"></a>11.4.错误信息英转中</h4><p><a target="_blank" rel="noopener" href="https://packagist.org/">https://packagist.org</a> 下载 laravel-lang</p>
<p>在项目根目录cmd输入说明里的安装指令:composer require caouecs&#x2F;laravel-lang:~3.0</p>
<p>语言包文件再vendor&#x2F;caous&#x2F;laravel-lang中，将你需要的语言目录复制到resources&#x2F;lang即可，一般找zh-cn</p>
<p>然后再config&#x2F;app.php中修改locale的值，改成你复制过来的语言包的文件夹名字</p>
<p>翻译不准可以去zh-cn文件夹下修改语言包文件代码</p>
<h3 id="12-分页"><a href="#12-分页" class="headerlink" title="12.分页"></a>12.分页</h3><p>表：news</p>
<p>建好模型</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">News</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$table</span>=<span class="string">&quot;news&quot;</span>;</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$primaryKey</span>=<span class="string">&#x27;id&#x27;</span>;</span><br><span class="line">    <span class="keyword">public</span> <span class="variable">$timestamps</span>=<span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">protected</span> <span class="variable">$fillable</span>=[<span class="string">&#x27;id&#x27;</span>,<span class="string">&#x27;title&#x27;</span>,<span class="string">&#x27;content&#x27;</span>,<span class="string">&#x27;image&#x27;</span>,<span class="string">&#x27;auther&#x27;</span>,<span class="string">&#x27;class&#x27;</span>,<span class="string">&#x27;time&#x27;</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>控制器</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">use</span> <span class="title">App</span>\<span class="title">News</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">homenews</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="variable">$homenews</span>=<span class="title class_">News</span>::<span class="title function_ invoke__">where</span>(<span class="string">&#x27;class&#x27;</span>,<span class="string">&#x27;3&#x27;</span>)-&gt;<span class="title function_ invoke__">orderBy</span>(<span class="string">&#x27;time&#x27;</span>,<span class="string">&#x27;desc&#x27;</span>)-&gt;<span class="title function_ invoke__">paginate</span>(<span class="number">10</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="title function_ invoke__">view</span>(<span class="string">&#x27;homenews&#x27;</span>,[<span class="string">&quot;homenews&quot;</span>=&gt;<span class="variable">$homenews</span>]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>视图</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;div <span class="class"><span class="keyword">class</span>=&quot;<span class="title">row</span> <span class="title">justify</span>-<span class="title">content</span>-<span class="title">center</span> <span class="title">mt</span>-4&quot;&gt;</span></span><br><span class="line"><span class="class">    &lt;<span class="title">div</span> <span class="title">class</span>=&quot;<span class="title">col</span>-<span class="title">md</span>-4 <span class="title">pl</span>-5&quot;&gt;</span></span><br><span class="line"><span class="class">		</span>&#123;&#123;<span class="variable">$hotdate</span>-&gt;<span class="title function_ invoke__">links</span>()&#125;&#125;</span><br><span class="line">	&lt;/div&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://example.com">学途路漫漫</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">http://example.com/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">学途路漫漫</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Php/">Php</a></div><div class="post_share"><div class="social-share" data-image="https://s1.ax1x.com/2023/04/03/pph4doQ.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.png" target="_blank"><img class="post-qr-code-img" src="/img/wechat.png" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.png" target="_blank"><img class="post-qr-code-img" src="/img/alipay.png" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/10/vue3%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" title="vue3快速上手"><img class="cover" src="https://user-images.githubusercontent.com/499550/93624428-53932780-f9ae-11ea-8d16-af949e16a09f.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">vue3快速上手</div></div></a></div><div class="next-post pull-right"><a href="/2020/06/25/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="git常用命令"><img class="cover" src="https://s1.ax1x.com/2023/04/03/pph46yV.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">git常用命令</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.gif" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">学途路漫漫</div><div class="author-info__description">今日事，今日毕</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">7</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/code-jay" target="_blank" title="Gitee"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1940879828@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=306898885" target="_blank" title="Music"><i class="fa-solid fa-music"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#laravel54%E7%AC%94%E8%AE%B0"><span class="toc-number">1.</span> <span class="toc-text">laravel54笔记</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%87%86%E5%A4%87%E5%89%8D"><span class="toc-number">1.0.1.</span> <span class="toc-text">1.准备前</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84%E5%88%86%E6%9E%90"><span class="toc-number">1.0.2.</span> <span class="toc-text">2.目录结构分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%90%AF%E5%8A%A8%E6%96%B9%E5%BC%8F"><span class="toc-number">1.0.3.</span> <span class="toc-text">3.启动方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-%E8%B7%AF%E7%94%B1"><span class="toc-number">1.0.4.</span> <span class="toc-text">4.路由</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%EF%BC%9Aroutes-x2F-web-php"><span class="toc-number">1.0.4.1.</span> <span class="toc-text">4.1.路由配置文件：routes&#x2F;web.php</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-%E8%B7%AF%E7%94%B1%E6%96%B9%E5%BC%8F"><span class="toc-number">1.0.4.2.</span> <span class="toc-text">4.2.路由方式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-3-%E8%B7%AF%E7%94%B1%E5%8F%82%E6%95%B0"><span class="toc-number">1.0.4.3.</span> <span class="toc-text">4.3.路由参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-4-%E8%B7%AF%E7%94%B1%E5%88%AB%E5%90%8D"><span class="toc-number">1.0.4.4.</span> <span class="toc-text">4.4.路由别名</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-5-%E8%B7%AF%E7%94%B1%E7%BE%A4%E7%BB%84"><span class="toc-number">1.0.4.5.</span> <span class="toc-text">4.5.路由群组</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-%E6%8E%A7%E5%88%B6%E5%99%A8"><span class="toc-number">1.0.5.</span> <span class="toc-text">5.控制器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-%E6%8E%A7%E5%88%B6%E5%99%A8%E7%94%9F%E6%88%90"><span class="toc-number">1.0.5.1.</span> <span class="toc-text">5.1.控制器生成</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1-%E6%8E%A7%E5%88%B6%E5%99%A8%E8%B7%AF%E7%94%B1"><span class="toc-number">1.0.5.2.</span> <span class="toc-text">5.1.控制器路由</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E6%8E%A7%E5%88%B6%E5%99%A8%E6%8E%A5%E5%8F%97%E7%94%A8%E6%88%B7%E8%BE%93%E5%85%A5"><span class="toc-number">1.0.6.</span> <span class="toc-text">6.控制器接受用户输入</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-DB%E7%B1%BB%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">1.0.7.</span> <span class="toc-text">7.DB类操作数据库</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#7-1-%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">1.0.7.1.</span> <span class="toc-text">7.1.配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-2-%E4%BD%BF%E7%94%A8"><span class="toc-number">1.0.7.2.</span> <span class="toc-text">7.2.使用</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A2%9E%E5%8A%A0%E4%BF%A1%E6%81%AF-insert"><span class="toc-number">1.0.7.2.1.</span> <span class="toc-text">增加信息(insert)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E4%BF%A1%E6%81%AF-update"><span class="toc-number">1.0.7.2.2.</span> <span class="toc-text">修改信息(update)</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE"><span class="toc-number">1.0.7.2.3.</span> <span class="toc-text">查询数据</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE-delete"><span class="toc-number">1.0.7.2.4.</span> <span class="toc-text">删除数据(delete)</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-%E8%A7%86%E5%9B%BE%E6%93%8D%E4%BD%9Cresources-x2F-views"><span class="toc-number">1.0.8.</span> <span class="toc-text">8.视图操作resources&#x2F;views</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1-%E8%A7%86%E5%9B%BE%E6%96%87%E4%BB%B6%E5%91%BD%E5%90%8D%E4%B8%8E%E6%B8%B2%E6%9F%93"><span class="toc-number">1.0.8.1.</span> <span class="toc-text">8.1.视图文件命名与渲染</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E5%8F%98%E9%87%8F%E5%88%86%E9%85%8D%E4%B8%8E%E5%B1%95%E7%A4%BA%EF%BC%88%E6%95%B0%E6%8D%AE%E4%BA%A4%E4%BA%92"><span class="toc-number">1.0.8.2.</span> <span class="toc-text">8.2.变量分配与展示（数据交互</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-3-%E8%A1%A5%E5%85%85%EF%BC%9Acompact%E5%87%BD%E6%95%B0%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-number">1.0.8.3.</span> <span class="toc-text">8.3.补充：compact函数传递参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-4-%E5%9C%A8%E8%A7%86%E5%9B%BE%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%87%BD%E6%95%B0"><span class="toc-number">1.0.8.4.</span> <span class="toc-text">8.4.在视图中使用函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-5-%E5%BE%AA%E7%8E%AF%E4%B8%8E%E5%88%86%E6%94%AF%E8%AF%AD%E6%B3%95%E6%A0%87%E7%AD%BE"><span class="toc-number">1.0.8.5.</span> <span class="toc-text">8.5.循环与分支语法标签</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-6-%E6%A8%A1%E6%9D%BF%E7%BB%A7%E6%89%BF-x2F-%E5%8C%85%E5%90%AB"><span class="toc-number">1.0.8.6.</span> <span class="toc-text">8.6.模板继承&#x2F;包含</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-7-%E5%A4%96%E9%83%A8%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6%E5%BC%95%E5%85%A5"><span class="toc-number">1.0.8.7.</span> <span class="toc-text">8.7.外部静态文件引入</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-CSRF%E6%94%BB%E5%87%BB"><span class="toc-number">1.0.9.</span> <span class="toc-text">9.CSRF攻击</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#9-1-%E4%BB%80%E4%B9%88%E6%98%AFCSRF%E6%94%BB%E5%87%BB"><span class="toc-number">1.0.9.1.</span> <span class="toc-text">9.1.什么是CSRF攻击</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E5%86%99%EF%BC%9A-csrf-field"><span class="toc-number">1.0.10.</span> <span class="toc-text">简写：{ {csrf_field()} }</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#9-2-CSRF%E9%AA%8C%E8%AF%81%E6%8E%92%E9%99%A4%E4%BE%8B%E5%A4%96%E8%B7%AF%E7%94%B1"><span class="toc-number">1.0.10.1.</span> <span class="toc-text">9.2.CSRF验证排除例外路由</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E6%A8%A1%E5%9E%8B%E6%93%8D%E4%BD%9C-AR%E6%A8%A1%E5%BC%8F"><span class="toc-number">1.0.11.</span> <span class="toc-text">10.模型操作(AR模式)</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#10-1-%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9E%8B"><span class="toc-number">1.0.11.1.</span> <span class="toc-text">10.1.定义模型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-2-%E6%A8%A1%E5%9E%8B%E6%8E%A7%E5%88%B6%E5%99%A8%E8%B0%83%E7%94%A8"><span class="toc-number">1.0.11.2.</span> <span class="toc-text">10.2.模型控制器调用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-3-%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="toc-number">1.0.11.3.</span> <span class="toc-text">10.3.添加数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-4-%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE"><span class="toc-number">1.0.11.4.</span> <span class="toc-text">10.4.查询数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-4-%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE"><span class="toc-number">1.0.11.5.</span> <span class="toc-text">10.4.修改数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#10-5-%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE"><span class="toc-number">1.0.11.6.</span> <span class="toc-text">10.5.删除数据</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#11-%E8%87%AA%E5%8A%A8%E9%AA%8C%E8%AF%81"><span class="toc-number">1.0.12.</span> <span class="toc-text">11.自动验证</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#11-1-%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C"><span class="toc-number">1.0.12.1.</span> <span class="toc-text">11.1.准备工作</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-2-%E9%AA%8C%E8%AF%81%E6%96%B9%E6%B3%95"><span class="toc-number">1.0.12.2.</span> <span class="toc-text">11.2.验证方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-3-%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E8%BE%93%E5%87%BA%EF%BC%88%E6%94%BE%E8%A7%86%E5%9B%BE%E6%96%87%E4%BB%B6%E9%87%8C%E5%A4%B4"><span class="toc-number">1.0.12.3.</span> <span class="toc-text">11.3.错误信息输出（放视图文件里头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#11-4-%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF%E8%8B%B1%E8%BD%AC%E4%B8%AD"><span class="toc-number">1.0.12.4.</span> <span class="toc-text">11.4.错误信息英转中</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#12-%E5%88%86%E9%A1%B5"><span class="toc-number">1.0.13.</span> <span class="toc-text">12.分页</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/03/10/vue3%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" title="vue3快速上手"><img src="https://user-images.githubusercontent.com/499550/93624428-53932780-f9ae-11ea-8d16-af949e16a09f.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="vue3快速上手"/></a><div class="content"><a class="title" href="/2023/03/10/vue3%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B/" title="vue3快速上手">vue3快速上手</a><time datetime="2023-03-10T06:20:40.000Z" title="发表于 2023-03-10 14:20:40">2023-03-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="laravel框架学习笔记"><img src="https://s1.ax1x.com/2023/04/03/pph4doQ.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="laravel框架学习笔记"/></a><div class="content"><a class="title" href="/2020/12/01/laravel%E6%A1%86%E6%9E%B6%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="laravel框架学习笔记">laravel框架学习笔记</a><time datetime="2020-12-01T12:06:29.000Z" title="发表于 2020-12-01 20:06:29">2020-12-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/06/25/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="git常用命令"><img src="https://s1.ax1x.com/2023/04/03/pph46yV.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="git常用命令"/></a><div class="content"><a class="title" href="/2020/06/25/git%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" title="git常用命令">git常用命令</a><time datetime="2020-06-25T08:00:29.000Z" title="发表于 2020-06-25 16:00:29">2020-06-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/06/22/Cookie%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8/" title="Cookie本地存储"><img src="https://s1.ax1x.com/2023/04/03/pph4Ysf.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Cookie本地存储"/></a><div class="content"><a class="title" href="/2020/06/22/Cookie%E6%9C%AC%E5%9C%B0%E5%AD%98%E5%82%A8/" title="Cookie本地存储">Cookie本地存储</a><time datetime="2020-06-22T07:47:29.000Z" title="发表于 2020-06-22 15:47:29">2020-06-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/06/05/php%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/" title="php入门笔记"><img src="https://s1.ax1x.com/2023/04/03/pph4qeO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="php入门笔记"/></a><div class="content"><a class="title" href="/2020/06/05/php%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/" title="php入门笔记">php入门笔记</a><time datetime="2020-06-05T08:27:40.000Z" title="发表于 2020-06-05 16:27:40">2020-06-05</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By 学途路漫漫</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">简</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>